package com.hyh.web;

import com.hyh.bean.Student;
import com.hyh.service.StudentService;
import com.hyh.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/stuNoServlet")
public class StuNoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuNo = req.getParameter("stuNo");
        String text = null;
        StudentService service = new StudentServiceImpl();
        List<Student> students = service.getStuNo();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        for (Student stu:students) {
            String num = stu.getStuNo();
            if (num.equals(stuNo)){
                text = "该学号已经被使用，请输入其它学号！";
                break;
            }else {
                text = "该学号可以使用！";
            }
        }
        writer.print(text);
    }
}
